﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using CapstoneProjectAEReader.DataAccess.DBAccess;
using CapstoneProjectAEReader.Entities.Models;
using Microsoft.Office.Interop.Word;
using CapstoneProjectAEReader.Common.Utility;

namespace CapstoneProjectAEReader.Business.Others
{
    public class EbookDetailExporter
    {
        #region Attributes

        // create MS-Word application
        private Application _msWord = new Application();

        // create Word document reference
        private Document _doc;

        // Create misssing value object
        private object _objMiss = Missing.Value;

        // Create directory
        private object _objFile;

        private const string EndOfDoc = "\\endofdoc";
        private const string TableOfNotes = "Table of Notes";
        private DAO _dao;

        #endregion

        #region Methods

        public Microsoft.Office.Interop.Word.Document wordDocument { get; set; }

        public EbookDetailExporter(DTO dto)
        {
            _dao = new DAO(dto);
        }

        public void CreateMsWord(DAO dao , int bookId, string fileName)
        {
            // add blank documnet in word application
            _objFile = fileName;
            _doc = _msWord.Documents.Add(ref _objMiss, ref _objMiss, ref _objMiss, ref _objMiss);
            Ebook ebook = dao.GetEbookById(bookId);
            // write book information
            EbookInformationWriter(ebook);
            List<Note> lstnote = dao.GetNoteOfEbook(ebook.Id);
            WriteAParagraph(TableOfNotes);
            NoteDetailInformationWriter(lstnote,ebook.Id);

            // save ms-word application
            _doc.SaveAs(ref _objFile, ref _objMiss, ref _objMiss, ref _objMiss, ref _objMiss,
                       ref _objMiss,
                       ref _objMiss, ref _objMiss, ref _objMiss, ref _objMiss, ref _objMiss,
                       ref _objMiss, ref _objMiss, ref _objMiss, ref _objMiss, ref _objMiss
                );

            _doc.Close(ref _objMiss, ref _objMiss, ref _objMiss);


            //pdf
            Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
            wordDocument = appWord.Documents.Open(fileName);
            wordDocument.ExportAsFixedFormat("D:\testpdf.pdf", WdExportFormat.wdExportFormatPDF);


            //
        }

        public void NoteDetailInformationWriter(List<Note> noteList, int bookId)
        {
            int counter = 1;
            // Create end of document object
            object endofdoc = EndOfDoc;

            // create table in word documnet
            Table table;

            // calculate the range of endofdocument
            Range wordRange = _doc.Bookmarks.get_Item(ref endofdoc).Range;

            // add table with document with number of row and column
            table = _doc.Content.Tables.Add(wordRange, noteList.Count + 1, 4, ref _objMiss, ref _objMiss);

            // set border visibility true by input 1 and false by input 0
            table.Borders.Enable = 1;
            table.Cell(1, 1).Range.Text = Constants.CommonCharacters.Sharp;
            table.Cell(1, 2).Range.Text = Constants.ExportSupporter.NoteHeader;
            table.Cell(1, 3).Range.Text = Constants.ExportSupporter.DescriptionHeader;
            table.Cell(1, 4).Range.Text = Constants.ExportSupporter.PageNoHeader;
            table.Rows[1].Range.Font.Bold = 1;
            table.Rows[1].Range.Font.Size = 15;
            // set text in each cell of table
            for(int i = 0 ; i < noteList.Count ; i++)
            {
                NoteDetail noteDetail = _dao.GetNoteDetail(noteList[i].NoteId, bookId);
                table.Rows[i + 2].Range.Cells[1].Range.Text = counter.ToString();
                counter++;
                table.Rows[i + 2].Range.Cells[2].Range.Text = noteList[i].NoteContent;
                table.Rows[i + 2].Range.Cells[3].Range.Text = noteDetail.Description;
                table.Rows[i + 2].Range.Cells[4].Range.Text = noteDetail.PageNumber.ToString();
            }
        }

        public void EbookInformationWriter(Ebook ebook)
        {
            // create first paragraph with reference name

            // add paragraph with document
            Paragraph para1 = _doc.Content.Paragraphs.Add(ref _objMiss);

            //set font style of paragraph
            para1.Range.Font.Bold = 1;

            // Set Font
            para1.Range.Font.Size = 20;
            para1.Range.Text = ebook.Title;

            // selection range of after insert paragraph
            para1.Range.InsertParagraphAfter();
            para1.Range.InlineShapes.AddHorizontalLineStandard();

            // create second paragraph with reference name

            // add paragraph with document
            Paragraph para2 = _doc.Content.Paragraphs.Add(ref _objMiss);

            // Set Font
            para2.Range.Font.Bold = 0;
            para2.Range.Font.Size = 11;

            // Write text of paragraph
            para2.Range.Text = Constants.ExportSupporter.Author + ebook.Author + Constants.CommonCharacters.NextLine;
            para2.Range.Text += Constants.ExportSupporter.ImportDate + ebook.ImportDate +
                                Constants.CommonCharacters.NextLine;
            para2.Range.Text += String.Format(Constants.ExportSupporter.Rating, ebook.Rating) +
                                Constants.CommonCharacters.NextLine;
            para2.Range.Text += Constants.ExportSupporter.Description + ebook.Description +
                                Constants.CommonCharacters.NextLine;

            // selection range of after insert paragraph
            para2.Range.InsertParagraphAfter();
            para2.Range.InlineShapes.AddHorizontalLineStandard();
        }

        public void WriteAParagraph(string text)
        {
            // create first paragraph with reference name
            Paragraph para1;

            // add paragraph with document
            para1 = _doc.Content.Paragraphs.Add(ref _objMiss);

            //set font style of paragraph
            para1.Range.Font.Bold = 1;

            // Set Font
            para1.Range.Font.Size = 15;
            para1.Range.Text = text;

            // selection range of after insert paragraph
            para1.Range.InsertParagraphAfter();

            //// create second paragraph with reference name
            //Paragraph para2;

            //// add paragraph with document
            //para2 = _doc.Content.Paragraphs.Add(ref _objMiss);

            //// Set Font
            //para2.Range.Font.Bold = 0;
            //para2.Range.Font.Size = 11;

            //// Write text of paragraph
            //para2.Range.Text = "- General Description :\r\n " + note.Description;

            //// selection range of after insert paragraph
            //para2.Range.InsertParagraphAfter();
        }

        #endregion
    }
}